package com.demo.zhlg.service.impl;

import com.demo.zhlg.entity.TaskOrders;
import com.demo.zhlg.entity.WalletTransactions;
import com.demo.zhlg.entity.Wallets;
import com.demo.zhlg.mapper.TaskOrdersMapper;
import com.demo.zhlg.mapper.WalletTransactionsMapper;
import com.demo.zhlg.mapper.WalletsMapper;
import com.demo.zhlg.util.Result;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.demo.zhlg.entity.Payments;
import com.demo.zhlg.mapper.PaymentsMapper;
import com.demo.zhlg.service.PaymentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.sqlite.date.DateFormatUtils;

import java.util.Date;

import static com.demo.zhlg.entity.table.PaymentsTableDef.PAYMENTS;
import static com.demo.zhlg.entity.table.WalletsTableDef.WALLETS;
import static com.demo.zhlg.entity.table.TaskOrdersTableDef.TASK_ORDERS;
import static com.demo.zhlg.entity.table.WalletTransactionsTableDef.WALLET_TRANSACTIONS;

/**
 *  服务层实现。
 *
 * @author lenovo
 * @since 2025-04-26
 */
@Service
public class PaymentsServiceImpl extends ServiceImpl<PaymentsMapper, Payments> implements PaymentsService {

    @Autowired
    private PaymentsMapper paymentsMapper;
    @Autowired
    private WalletTransactionsMapper walletTransactionsMapper;
    @Autowired
    private WalletsMapper walletsMapper;
    @Autowired
    private TaskOrdersMapper taskOrdersMapper;

    private int getUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        return loginUser.getUser().getUserId();
    }

    @Override
    public Object submitPayment(Payments payment) {
        payment.setUserId(getUserId());
        Wallets eWallet = walletsMapper.selectOneByQuery(
                new QueryWrapper()
                        .where(WALLETS.USER_ID.eq(getUserId()))
        );
        TaskOrders taskOrder = taskOrdersMapper.selectOneById(payment.getOrderId());
        WalletTransactions eWalletTransaction = new WalletTransactions();
        eWalletTransaction.setUserId(getUserId());
        eWalletTransaction.setWalletId(eWallet.getWalletId());
        eWalletTransaction.setRelatedId(taskOrder.getWorkerId());
        eWalletTransaction.setTransactionType(1);
        eWalletTransaction.setTransactionAmount(payment.getPaymentAmount());
        eWalletTransaction.setBalance(eWallet.getBalance() - payment.getPaymentAmount());
        eWalletTransaction.setTransactionStatus(1);
        eWalletTransaction.setCreateTime(DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        eWalletTransaction.setUpdateTime(DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        walletTransactionsMapper.insert(eWalletTransaction);
        eWalletTransaction = walletTransactionsMapper.selectOneByQuery(
                new QueryWrapper()
                        .where(WALLET_TRANSACTIONS.USER_ID.eq(eWallet.getUserId()))
                        .orderBy(WALLET_TRANSACTIONS.TRANSACTION_ID, false)
        );
        payment.setTransactionId(eWalletTransaction.getTransactionId());
        payment.setPaymentTime(DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        payment.setCreateTime(DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        payment.setUpdateTime(DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        payment.setNotifyTime(DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        paymentsMapper.insert(payment);
        payment = paymentsMapper.selectOneByQuery(
                new QueryWrapper()
                        .where(PAYMENTS.USER_ID.eq(eWallet.getUserId()))
                        .orderBy(PAYMENTS.PAYMENT_ID, false)
        );
        eWallet.setBalance(eWallet.getBalance() - payment.getPaymentAmount());
        eWallet.setUpdateTime(DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        walletsMapper.update(eWallet);

        Wallets wWallet = walletsMapper.selectOneByQuery(
                new QueryWrapper()
                        .where(WALLETS.USER_ID.eq(taskOrder.getWorkerId()))
        );
        WalletTransactions wWalletTransaction = new WalletTransactions();
        wWalletTransaction.setUserId(taskOrder.getWorkerId());
        wWalletTransaction.setWalletId(wWallet.getWalletId());
        wWalletTransaction.setRelatedId(getUserId());
        wWalletTransaction.setTransactionType(0);
        wWalletTransaction.setTransactionAmount(payment.getPaymentAmount());
        wWalletTransaction.setBalance(wWallet.getBalance() + payment.getPaymentAmount());
        wWalletTransaction.setTransactionStatus(1);
        wWalletTransaction.setCreateTime(DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        wWalletTransaction.setUpdateTime(DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        walletTransactionsMapper.insert(wWalletTransaction);
        wWallet.setBalance(wWallet.getBalance() + payment.getPaymentAmount());
        wWallet.setUpdateTime(DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        walletsMapper.update(wWallet);
        taskOrder.setOrderStatus(1);
        taskOrder.setPaymentStatus(1);
        taskOrdersMapper.update(taskOrder);
        return Result.success(payment);
    }
}
